﻿using System;
using System.Windows;
using System.Linq;
using Buddy.Common.Math;
using Buddy.Navigation;
using Buddy.Common.Plugins;
using Buddy.BehaviorTree;
using Buddy.CommonBot;
using Buddy.Common;
using Buddy.CommonBot.Logic;
using Buddy.Swtor;
using Buddy.Swtor.Objects;
using System.Collections.Generic;

using Action = Buddy.BehaviorTree.Action;

namespace Buddywing.Plugins
{
    public class ChestinatorPlugin : IPlugin
    {
        #region Implementation of IEquatable<IPlugin>

        public bool Equals(IPlugin other)
        {
            return other.Name == Name;
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "Neo93"; } }

        public Version Version { get { return new Version(1,0); } }

        public string Name { get { return "Chestinator"; } }

        public string Description { get { return "Moves to chests and opens them"; } }

        public Window DisplayWindow { get { return null; } }

        public TorPlaceable Chest { get { return ObjectManager.GetObjects<TorPlaceable>().FirstOrDefault(o => o.Name.Contains("Chest") && o.Distance <= 4f); } }

        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
            if (BuddyTor.Me.InCombat)
                return;
        }

        /// <summary> Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) </summary>
        public void OnInitialize()
        {
            Logging.Write("[Chestinator] Let's earn some $$$");
            TreeHooks.Instance.InsertHook("QuestProfileBehavior_Hook", 0,
            new Decorator(ret => Chest != null,
            new PrioritySelector(
                new Decorator(ret => Chest.Distance <= 0.4f,
                    new Sequence(
                        new Action(ret => Chest.Interact()),
                            new Action(ret => Logging.Write("[Chestinator] Looted " + Chest.Name)))),
                new Decorator(ret => Chest.Distance > 0.4f,
                    new Sequence(
                        new Action(ret => CommonBehaviors.MoveAndStop(when => Chest.Position, 0.4f, true)),
                            new Action(ret => Logging.Write("[Chestinator] Move to " + Chest.Name + " ( " + Chest.Position + " )"))))))
                            );
        }

        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
            //TreeHooks.Instance.RemoveHook("QuestProfileBehavior_Hook", 0);
        }

        #endregion
    }
}
